<template>
  <router-view/>
</template>
<script setup>
import {onMounted, ref} from "vue";

let beforeUnloadTime = ref(0)
let gapTime = ref(0)

const beforeunloadHandler = async () => {
  beforeUnloadTime.value = new Date().getTime();
}

const unloadHandler = async () => {
  gapTime.value = new Date().getTime() - beforeUnloadTime.value;
  //判断是窗口关闭还是刷新
  if (gapTime.value <= 5) {//浏览器关闭
    localStorage.clear()
    console.log('111')
  }else {
    console.log('222')
  }
}

onMounted(() => {
  //监听页面卸载（关闭）或刷新

  window.addEventListener('beforeunload', () => beforeunloadHandler())

  window.addEventListener('unload', () => unloadHandler())
})
</script>
<style>
</style>
